<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <!-- import CSS -->
  <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
</head>
<body>
<div id="app">
  <el-page-header style="background-color: #0095d7;color: white;line-height: 60px" @back="goBack" content="添加商品">
  </el-page-header>
  <el-card style="height: 700px;width: 600px;margin: 0 auto">
  <el-form  ref="form" label-width="80px">
    <el-form-item label="商品标题">
      <el-input v-model="p.title"></el-input>
    </el-form-item>
    <el-form-item label="商品价格">
      <el-input v-model="p.price"></el-input>
    </el-form-item>
    <el-form-item label="商品原价">
      <el-input v-model="p.oldPrice"></el-input>
    </el-form-item>
    <el-form-item label="销量">
      <el-input v-model="p.saleCount"></el-input>
    </el-form-item>
    <el-form-item label="库存">
      <el-input v-model="p.num"></el-input>
    </el-form-item>
    <el-form-item label="商品分类">
      <el-select v-model="p.categoryId" placeholder="请选择">
        <el-option v-for="c in categoryArr"
                    :label="c.name":value="c.id"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="商品图片">
      <el-upload
              name="pic"
              limit="1"
              action="/upload"
              list-type="picture-card"
              :on-preview="handlePictureCardPreview"
              :on-success="handleSuccess"
              :on-remove="handleRemove">
        <i class="el-icon-plus"></i>
      </el-upload>
      <el-dialog :visible.sync="dialogVisible">
        <img width="100%" :src="dialogImageUrl" alt="">
      </el-dialog>
    </el-form-item>
    <el-form-item>
      <el-button @click="insert()">添加商品</el-button>
    </el-form-item>
  </el-form>
  </el-card>
</div>
</body>
<!--引入Axios框架-->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<!-- import Vue before Element -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
<script>
  let v = new Vue({
    el: '#app',
    data: function() {
      return {
        dialogImageUrl: '',
        dialogVisible: false,
        p:{
          title:"",
          price:"",
          oldPrice:"",
          saleCount:"",
          num:"",
          categoryId:"",
          url:"",
        },
        categoryArr:[],
      }
    },
    methods: {
      goBack() {
        console.log('go back');
        history.back()
      },
      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePictureCardPreview(file) {
        this.dialogImageUrl = file.url;
        this.dialogVisible = true;
      },
      //记得复习
      handleSuccess(response,file,fileList){
            v.p.url=response
      },
      insert(){
      if (v.p.url==""){
        v.$message.error("请选择上传的图片")
        return
      }
      axios.post("/product/insert",v.p).then(function () {
        location.href="/admin.html"
      })
      }
    },
    created:function () {
      //动态回显
      axios.get("/category/select").then(function (response) {
        v.categoryArr=response.data;
      })

    }
  })
</script>
</html>